package com.jinxin.phonecloudmusic.myapplication.fragment.fra;

import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.jinxin.phonecloudmusic.myapplication.R;
import com.jinxin.phonecloudmusic.myapplication.adapter.MusicDownListAdapter;
import com.jinxin.phonecloudmusic.myapplication.base.BaseFragment;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicDown;
import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;
import com.jinxin.phonecloudmusic.myapplication.utils.RxBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ZJ on 2017/10/30 0030.
 * 音乐下载
 */

public class MusicDownLoadFragment extends BaseFragment{
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.lv_music_download)
    ListView lvMusicDownload;

    private List<String> dataMusicName;
    private List<String> musicName;//音乐名字
    private List<String> songerName;//歌手名字
    private List<MusicDown> musicDownList;
    private MusicDownListAdapter musicDownListAdapter;
    @Override
    public View inflate(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fra_music_download, container, false);
    }

    @Override
    public void initData() {
        //遍历下载的文件夹里面的歌曲,适配数据
        getLocalMusic();
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getDefault().post(new MusicControlEvent(36,""));
            }
        });
        lvMusicDownload.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RxBus.getDefault().post(new MusicControlEvent(1,musicDownList,position));
            }
        });
    }

    private void getLocalMusic(){
        //检测SD卡是否存在
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            //File root = Environment.getExternalStorageDirectory();
            File root=new File("/mnt/sdcard/file");
            getSDFile(root);
        } else {
            Toast.makeText(getActivity(), "没有SD卡", Toast.LENGTH_LONG).show();
        }
    }

    // 遍历接收一个文件路径，然后把文件子目录中的所有文件遍历并输出来
    private void getSDFile(File root) {
        dataMusicName = new ArrayList<>();
        musicName=new ArrayList<>();
        songerName=new ArrayList<>();
        musicDownList=new ArrayList<>();
        File files[] = root.listFiles();
        //为空的文件夹，不做任何动作
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory())//判断是否是文件夹
                {
                    getSDFile(f);

                } else {
                    if (f.getPath().endsWith(".mp3")) {
                        //拆分字符串
                        String[] name = f.getPath().split("/");
                        for (String a : name) {
                            if (a.endsWith(".mp3")) {
                                //后来-刘若英.mp3
                                String[] strArr=a.split("-");
                                if (strArr.length==2){
                                    dataMusicName.add(strArr[0]+".mp3");
                                    String[] strSong=strArr[1].split("\\.");
                                    songerName.add(strSong[0]);
                                    MusicDown musicDown=new MusicDown(strArr[0]+".mp3",strSong[0]);
                                    musicDownList.add(musicDown);
                                }
                            }
                        }
                        //音乐
                        Log.i("mp3", f.getPath());//输出音乐路径

                    } else if (f.getPath().endsWith(".jpg")) {
                        //jpg图片
                        Log.i("Img", f.getPath());//输出图片路径
                    } else if (f.getPath().endsWith(".txt")) {
                        //文本
                        Log.i("Txt", f.getPath());//t文本
                    }/*else if(){
                        //其他
                    }*/
                }

            }
        }

        //适配数据
        if (null==musicDownListAdapter){
            musicDownListAdapter=new MusicDownListAdapter(getActivity(),R.layout.item_home_page_music_list,musicDownList);
        }else {
            musicDownListAdapter.replaceAll(musicDownList);
        }
        lvMusicDownload.setAdapter(musicDownListAdapter);
    }
}
